# -*- utf-8 -*-
"""
全局中间件
"""
from ajson import aj
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin

from apps.basic_data.models import ViewRecord
from apps.basic_data.serializers import UserDefaultSerializer
from utils.network import Result
from wangshihe.settings import MY_DEBUG


class RequestAccLog(MiddlewareMixin):

    def process_request(self, request):
        # print("md1  process_request 方法", id(request))  # 在视图之前执行

        path_info = ''
        remote_addr = ''
        http_host = ''
        http_sec_ch_ua = ''
        http_user_agent = ''
        http_sec_ch_ua_platform = ''
        http_origin = ''
        http_referer = ''
        username = ''

        try:
            path_info = request.META.get('PATH_INFO')
        except:
            pass
        try:
            remote_addr = request.META.get('REMOTE_ADDR')
        except:
            pass
        try:
            http_host = request.META.get('HTTP_HOST')
        except:
            pass
        try:
            http_sec_ch_ua = request.META.get('http_sec_ch_ua')
        except:
            pass
        try:
            http_user_agent = request.META.get('HTTP_USER_AGENT')
        except:
            pass
        try:
            http_sec_ch_ua_platform = request.META.get('HTTP_SEC_CH_UA_PLATFORM')
        except:
            pass
        try:
            http_origin = request.META.get('HTTP_ORIGIN')
        except:
            pass
        try:
            http_referer = request.META.get('HTTP_REFERER')
        except:
            pass
        try:
            username = request.META.get('USERNAME')
        except:
            pass
        vr = ViewRecord(path_info=path_info,
                        remote_addr=remote_addr,
                        http_host=http_host,
                        http_sec_ch_ua=http_sec_ch_ua,
                        http_user_agent=http_user_agent,
                        http_sec_ch_ua_platform=http_sec_ch_ua_platform,
                        http_origin=http_origin,
                        http_referer=http_referer,
                        username=username)
        vr.save()

    def process_response(self, request, response):
        # 基于请求响应
        # print("md1  process_response 方法！", id(request))  # 在视图之后
        if hasattr(response, 'data'):
            response.data.setdefault('code', response.status_code)
        return response

    def process_exception(self, request, exception):  # 引发错误 才会触发这个方法
        print(str(exception))
        if MY_DEBUG:
            raise exception
        return HttpResponse(aj.parse({
            'code': 500,
            'message': str(exception),
            'form_data': {
                'GET': request.GET,
                'POST': request.POST
            }}))


def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'code': 200,
        'message': 'ok',
        'data': {
            'token': 'JWT ' + token,
            'user': UserDefaultSerializer(user).data
        }
    }
